import { login, logout, getInfo, loginForChange } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg'
import { captchaLogin } from '@/api/callCenter'

const useUserStore = defineStore('user', {
  state: () => ({
    token: getToken(),
    name: '',
    avatar: '',
    nickName: '',
    deptName: '',
    city: '',
    roles: [],
    user: {},
    userInfo: {
      listDisaplayObj: {}
    },
    permissions: [],
    isAdmin: false, //是否是admin账号
    initpassword: '',
    homeSetting: '', // 首页组件
    msgNumber: 0,
    todoList: []
  }),

  actions: {
    identityLogin(userInfo) {
      const username = userInfo.username.trim()
      return new Promise<void>((resolve, reject) => {
        loginForChange(username)
          .then(res => {
            setToken(res.data.access_token)
            // @ts-ignore
            this.token = res.token
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    //电话记录单 -授权登录
    captchaLoginFn(userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      return new Promise<void>((resolve, reject) => {
        captchaLogin(username, password)
          .then(res => {
            setToken(res.data.access_token)
            // @ts-ignore
            this.token = res.token
            this.initpassword = res.data.initpassword
              ? res.data.initpassword
              : ''
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 登录
    login(userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise<void>((resolve, reject) => {
        login(username, password, code, uuid)
          .then(res => {
            setToken(res.data.access_token)
            // @ts-ignore
            this.token = res.token
            this.initpassword = res.data.initpassword
              ? res.data.initpassword
              : ''
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 获取用户信息
    getInfo() {
      return new Promise((resolve, reject) => {
        getInfo()
          .then(res => {
            // @ts-ignore
            const user = res.user
            // const avatar = (user.avatar == "" || user.avatar == null) ? defAva : import.meta.env.VITE_APP_BASE_API + user.avatar;
            const avatar =
              user.avatar == '' || user.avatar == null ? defAva : user.avatar
            // @ts-ignore
            if (res.roles && res.roles.length > 0) {
              // 验证返回的roles是否是一个非空数组
              // @ts-ignore
              this.roles = res.roles
              // @ts-ignore
              this.permissions = res.permissions
            } else {
              // @ts-ignore
              this.roles = ['ROLE_DEFAULT']
            }
            console.log(user.admin, 'user.admin--')
            this.userInfo = user
            this.name = user.userName
            this.city = user.cityCode
            this.avatar = avatar
            this.user = user
            this.isAdmin = user.admin
            this.homeSetting = user.homeSetting
            resolve(res)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 退出系统
    logOut() {
      return new Promise<void>((resolve, reject) => {
        // @ts-ignore
        logout(this.token)
          .then(() => {
            this.token = ''
            this.roles = []
            this.permissions = []
            sessionStorage.removeItem('identityUserName')
            removeToken()
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 退出系统
    changeLogOut() {
      return new Promise<void>((resolve, reject) => {
        // @ts-ignore
        logout(this.token)
          .then(() => {
            this.token = ''
            this.roles = []
            this.permissions = []
            removeToken()
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    uploadAvatar(avatar) {
      this.avatar = avatar
    },
    updateMsgNumber(num) {
      this.msgNumber = num
    },
    updateTodoList(array) {
      this.todoList = array
    }
  }
})

export default useUserStore
